﻿using LD.Host;
using System;

namespace LD.Tool
{
    public partial class ToolPlcWriteFrm : FrmToolBase
    {
        public ToolPlcWriteFrm()
        {
            InitializeComponent();
            this.Shown += ToolMesSharpFrm_Shown;
            this.btn_ok.Click += Btn_ok_Click;
        }

        private void ToolMesSharpFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            if (TB_Tool.Param != null)
            {
                var param = Newtonsoft.Json.JsonConvert.DeserializeObject<PLCWriteParam>(TB_Tool.Param);
                if (param != null)
                {
                    this.txt_plcAddress.Text = param.Address;
                    this.cob_dataType.Text= param.DataType;
                    this.txt_Val.Text = param.Value;
                }
            }
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            var param = new PLCWriteParam()
            {
                DataType = this.cob_dataType.Text,
                Value = this.txt_Val.Text,
                Address=this.txt_plcAddress.Text,
            };

            TB_Tool.Param = Newtonsoft.Json.JsonConvert.SerializeObject(param);
        }
    }
}
